﻿#if DEBUG
namespace Test.Serializer {

    using LiteByte;
    using LiteByte.Converters;
    using Test.Entity;

    public class ClassTestEntitySerializer : ILBSerializer<ClassTestEntity> {

        public static byte[] Serialize(ClassTestEntity entity) {
            var writer = new LBWriter();
            Write(writer, entity);
            return writer.ToBytes();
        }

        public static ClassTestEntity Deserialize(byte[] bytes) {
            var reader = new LBReader(bytes);
            return Read(reader);
        }

        public static void Write(LBWriter writer, ClassTestEntity entity) {
            if (entity == null) {
                writer.WriteBit1(true);
                return;
            }
            writer.WriteBit1(false);
            UserEntitySerializer.Write(writer, entity.user);
            UserEntitySerializer.Write(writer, entity.userNull);
            PetEntitySerializer.Write(writer, entity.pet);
            PetEntitySerializer.Write(writer, entity.petNull);
        }


        public static ClassTestEntity Read(LBReader reader) {
            if (reader.ReadBit1()) return null;
            var entity = new ClassTestEntity();
            entity.user = UserEntitySerializer.Read(reader);
            entity.userNull = UserEntitySerializer.Read(reader);
            entity.pet = PetEntitySerializer.Read(reader);
            entity.petNull = PetEntitySerializer.Read(reader);
            return entity;
        }

    }

}
#endif
